<template>
	<view>
		<view class="" v-if="pageType == 0">
			<view class="">
				需提交法人手持身份证人像面照片
			</view>
			<view class="container">
				<view class="preview" @click="chooseImage(0)">
					<image v-if="legalImg" :src="legalImg" mode="aspectFill"></image>
					<view class="inner" v-else>
						<view style="display: flex;flex-direction: column;">
							<text>
								点击上传法人手持身份证人像面
							</text>
						</view>
					</view>
				</view>
				<view style="margin-top: 20rpx; width: 50%;">
					<u-button type="primary" text="提交" @click="subclaim(0)"></u-button>
				</view>
			</view>
		</view>
		<view class="" v-if="pageType == 1">
			<view class="">
				需提交授权委托书和被授权人身份证正反面
			</view>
			<view class="container" style="margin-top: 20rpx;display: flex;">
				<view class="" style="display: flex;justify-content: space-between;width: 80%;">
					<!-- <view class=""><u-button text="点击上传企业授权书" @click="chooseFile"></u-button></view> -->
					<view class=""><u-button text="授权书下载" @click="downloadsqs"></u-button></view>
				</view>
			</view>
			<view class="container">
				<view class="preview" @click="chooseImage(3)">
					<image v-if="sqs" :src="sqs" mode="aspectFill"></image>
					<view class="inner" v-else>
						<view style="display: flex;flex-direction: column;">
							<text>
								点击上传授权书（图）
							</text>
						</view>
					</view>
				</view>
			</view>
			<view class="container">
				<view class="preview" @click="chooseImage(1)">
					<image v-if="tarrantIdcardFont" :src="tarrantIdcardFont" mode="aspectFill"></image>
					<view class="inner" v-else>
						<view style="display: flex;flex-direction: column;">
							<text>
								点击上传身份证人像面
							</text>
						</view>
					</view>
				</view>
				<view class="preview" @click="chooseImage(2)">
					<image v-if="tarrantIdcardBack" :src="tarrantIdcardBack" mode="aspectFill"></image>
					<view class="inner" v-else>
						<view style="display: flex;flex-direction: column;">
							<text>
								点击上传身份证国徽面
							</text>
						</view>
					</view>
				</view>

				<view style="margin-top: 20rpx; width: 50%;">
					<u-button type="primary" text="提交" @click="subclaim(1)"></u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Index from "@/common/index-model.js"
	import fun from "@/common/publicFun.js"
	let index = new Index()
	let that = this
	export default {
		data() {
			return {
				enterpriseId: 0,
				pageType: 0,
				legalImg: '',
				tempFilePath: '',
				// tempFilesname: '',
				tarrantIdcardFont: '',
				tarrantIdcardBack: '',
				// enterpriseTarrant: ''
				sqs:''
			}
		},
		onLoad(opt) {
			console.log(opt)
			this.enterpriseId = opt.entid
			this.pageType = opt.type
			console.log(this.enterpriseId, this.pageType)
		},
		methods: {
			//提交按钮
			subclaim(type) {
				let sends ={}
				if (type == 0) {
					sends = {
						"enterpriseId": this.enterpriseId,
						"faceAuthPicture": this.legalImg
					}
				}
				if (type == 1) {
					sends = {
						"enterpriseId": this.enterpriseId,
						"enterpriseTarrant": this.sqs,
						"tarrantIdcardFont": this.tarrantIdcardFont,
						"tarrantIdcardBack": this.tarrantIdcardBack,
					}
				}
				index.getPublicUserMember(sends, 'client/realauth/claimEnterprise', 'post', res => {
					if (res.data.code == 200 && res.data.success) {
						console.log(res.data)
						index.show_tips("已提交");
						setTimeout(() => {
							uni.navigateBack();
						}, 1000)
					} else {
						index.show_tips(res.data.message);
						return false
					}
				})
			},

			// 选择图片
			chooseImage(type) {
				uni.chooseImage({
					count: 1, // 最多选择一张图片
					sizeType: ['compressed'], // 压缩图片
					sourceType: ['album', 'camera'], // 选择图片的来源，相册或者相机
					success: (res) => {
						console.log("选择图片", res.tempFilePaths)
						this.tempFilePath = res.tempFilePaths;
						if (type == 0) {
							this.legalImg = res.tempFilePaths[0];
						}
						if (type == 1) {
							this.tarrantIdcardFont = res.tempFilePaths[0];
						}
						if (type == 2) {
							this.tarrantIdcardBack = res.tempFilePaths[0];
						}
						if (type == 3) {
							this.sqs = res.tempFilePaths[0];
						}
						// 
						uni.uploadFile({
							url: index.img_url + 'client/public/upload?type=enterpriseClaim&file=' +
								this.tempFilePath,
							filePath: this.tempFilePath[0],
							name: 'file',
							formData: {
								// 可以添加额外的表单数据
							},
							header: {
								'X-Access-Token': uni.getStorageSync('token'),
								'Login-Type': 'client'
							},
							success: (res) => {
								if (res.statusCode === 200) {
									let imgurl = JSON.parse(res.data).result.url
									if (type == 0) {
										this.legalImg = imgurl
									}
									if (type == 1) {
										this.tarrantIdcardFont = imgurl
									}
									if (type == 2) {
										this.tarrantIdcardBack = imgurl
									}
									if (type == 3) {
										this.sqs = imgurl
									}
								} else {
									if (res.statusCode === 413) {
										index.show_tips('文件过大，请重新上传');
									} else {
										index.show_tips('服务器返回错误，请稍后再试');
									}
								}
							},
							fail: (err) => {
								index.show_tips('上传失败,请重新上传营业执照')
								// 上传失败的处理逻辑
								console.error('上传失败,请重新上传营业执照', err);
							}
						})
						// 
					}
				});
			},
			downloadsqs(){
				console.log("下载")
				const url = 'http://xgr.oss-cn-hangzhou.aliyuncs.com/upload/letterOfAuthorization/shouquanshu.docx';
				 uni.downloadFile({
				    url: url,
				    success: (res) => {
				      if (res.statusCode === 200) {
				        const tempFilePath = res.tempFilePath;
						console.log(res)
				        uni.openDocument({
				          filePath: tempFilePath,
				          showMenu: true, // 显示分享菜单
				          success: () => {
				            console.log('文件打开成功');
				          },
				          fail: (err) => {
				            console.error('文件打开失败：', err);
				          }
				        });
				        // 如果需要保存文件到本地，可以调用 uni.saveFile
				        // uni.saveFile({
				        //   tempFilePath: tempFilePath,
				        //   success: (saveRes) => {
				        //     console.log('文件保存成功，路径：', saveRes.savedFilePath);
				        //   }
				        // });
				      }
				    },
				    fail: (err) => {
				      console.error('文件下载失败：', err);
				    }
				  })
			}
		}
	}
</script>

<style>
	page {
		background: #f3f3f3;
	}
</style>
<style scoped>
	@import url("@/static/css/publics.css");
	@import url("@/static/css/myeditphone.css");

	.preview {
		margin-top: 40rpx;
		border-radius: 10rpx;
		height: 300rpx;
		width: 80%;
		display: flex;
		align-items: center;
		justify-content: center;
		text-align: center;
		background-color: #fff;
		border: 0;
		overflow: hidden;
		box-sizing: border-box;
	}
</style>